/**
 * Created by gongxun on 16/11/18.
 */

var KwxRecordLayer = KwxSecondBase.extend({
    ListView_content : null,
    mPlayListForJuIdList : null,
    ctor : function () {
        this._super();
        this.loadSecondCCS("res/ccs/KwxRecordLayer.json");
        this.setTitleText("res/ccs/hall_second/text/text_lishizhanji.png");

        this.mPlayListForJuIdList = [];
        this.ListView_content = mo.seekWidgetByName(this.ccsNode, "ListView_content");
        // this.ListView_content.setScrollBarEnabled(false);

        //查看其他人的录像
        mo.seekWidgetByName(this.ccsNode, "Button_ckzj").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                mo.mainLayer.addChild(new KwxRecordOtherLayer());
            }
        }, this);

        //加载模版
        this.loadItemTemple();

        this.updateShowIsNull();

        //修复剪切bug
        mo.gameHelper.setClippingLayout(this.ListView_content);
    },

    onEnter : function(){
        this._super();

        mo.showWaitLayer();
        mo.seekWidgetByName(this.ccsNode, "Text_tishi").setVisible(false);

        this.scheduleOnce(function(){
            //获取用户打牌列表
            mo.gameMsgManager.sendPlayListRequest(mo.gameUserInfo.uid);
        }, 0.5);

        mo.eventHelper.add("PlayListResponse", this, function(PLS){
            mo.closeWaitLayer();
            this.setPlayList(PLS);
        });
    },

    onExit : function(){
        this._super();
        mo.eventHelper.remove(this);
    },

    loadItemTemple : function(){
        var temple = mo.seekWidgetByName(this.ccsNode, "Panel_temple");
        this.ListView_content.setItemModel(temple);
        this.ListView_content.removeAllChildren();
    },

    addListViewItem : function(){
        this.ListView_content.pushBackDefaultItem();
        var ui = this.ListView_content.getItem(this.ListView_content.getItems().length - 1);
        var size = cc.size(this.ListView_content.getContentSize().width, 130);

        ui.setContentSize(size);
        var height = mo.gameHelper.projectCode == 2 ? 120 : 130;
        mo.seekWidgetByName(ui, "Image_bg").setContentSize(cc.size(size.width, height));
        mo.seekWidgetByName(ui, "Button_see").setPositionX(size.width - 20);

        return ui;
    },

    setPlayList : function(playList){
        this.mPlayListForJuIdList = mo.recordDetailedHelper.getSortRecordArray(playList);

        var loseColor = cc.color(40, 62, 144);
        var winColor = cc.color(182, 113, 0);

        for (var i = 0; i < this.mPlayListForJuIdList.length; i++){
            var ui = this.addListViewItem();
            var Button_see = mo.seekWidgetByName(ui, "Button_see");
            Button_see.setTag(i);
            Button_see.addTouchEventListener(this.onSeeTouch, this);

            var playList = this.mPlayListForJuIdList[i].items;
            var fristPlayItem = playList[0];

            var nicks = fristPlayItem.Nick;
            var uids = fristPlayItem.Uid;
            var faceUrls = fristPlayItem.FaceUrl;
            var scores = [0, 0, 0, 0, 0];

            var mySeat = 0;
            for (var seat = 0; seat < uids.length; seat++){
                if (uids[seat] == mo.gameUserInfo.uid){
                    mySeat = seat;
                    break;
                }
            }

            for (var j = 0; j < playList.length; j++){
                var playItem = playList[j];
                for(var x = 0; x < uids.length; x++){
                    scores[x] += playItem.Score[x];
                }
            }

            //游戏类型
            var gameId = fristPlayItem.GameId;
            var Panel_gameType = null;

            if (gameId == 14 || gameId == 15){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(true);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(false);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeKwx");
            }else if (gameId == 9){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 21){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_gsbdx.png");
            }else if (gameId == 22){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_psz.png");
            }else if(gameId == 20){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_whmj.png");
            }else if(gameId == 23){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_dzpk.png");
            }else if(gameId == 25){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_dtj.png");
            }else if(gameId == 26){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(true);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(false);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeKwx");

                mo.gameHelper.loadTextureImage(mo.seekWidgetByName(Panel_gameType, "Image_type"), "res/ccs/hall_second/roomCreateNew/img_btn_pdk.png");
            }

            for (var j = 0; j < uids.length; j++){
                var Panel_player = mo.seekWidgetByName(Panel_gameType, "Panel_player" + j);
                Panel_player.setVisible(true);
                var getSeat = (j + mySeat) % uids.length;

                var Text_player = mo.seekWidgetByName(Panel_player, "Text_player0");
                mo.gameHelper.setTextString(Text_player, mo.gameHelper.getUserNickFilterForCount(nicks[getSeat], 4));

                var Text_score = mo.seekWidgetByName(Panel_player, "Text_score0");
                mo.gameHelper.setTextString(Text_score, scores[getSeat] + "");
                if (gameId == 9 || gameId == 21 || gameId == 22 || gameId == 20 || gameId == 23 || gameId == 25){
                    Text_score.setPositionX(25 + Text_player.getContentSize().width);
                }

                Text_score.setTextColor(scores[getSeat] >= 0 ? winColor : loseColor);

                //head
                mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(Panel_player, "Image_head"), faceUrls[getSeat]);
            }

            //输赢情况
            var Image_losewin = mo.seekWidgetByName(ui, "Image_losewin");
            if (scores[mySeat] >= 0){
                mo.gameHelper.loadTextureImage(Image_losewin, "res/ccs/hall_second/record/win.png");
            }else{
                mo.gameHelper.loadTextureImage(Image_losewin, "res/ccs/hall_second/record/lose.png");
            }
        }
        this.updateShowIsNull();
    },

    onSeeTouch : function(sender, type){
        if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
            mo.showWaitLayer();
            var index = sender.getTag();
            var playList = this.mPlayListForJuIdList[index];
            this.addChild(new KwxRecordInfoLayer(playList));
        }
    },

    updateShowIsNull : function(){
        var Text_tishi = mo.seekWidgetByName(this.ccsNode, "Text_tishi");
        if (this.mPlayListForJuIdList.length == 0){
            Text_tishi.setVisible(true);
        }else{
            Text_tishi.setVisible(false);
        }
    }
});